<?php

namespace App\Http\Controllers\NewAdmin;

use App\Http\Controllers\AdapterWebController;
use App\Modules\Setting\SettingCategory;
use Illuminate\Http\Request;

class SettingCate extends AdapterWebController
{
    /**
     * 添加分类
     * @param Request $request
     * @return array
     * @throws \App\Exceptions\RequestFailException
     */
    public function cateList(Request $request)
    {
        $list = $this->appLayerExec(SettingCategory::class, 'getTree');
        return view('NewAdmin.setting.setting_cate_list', ['list' => $list['res']]);
    }

    /**
     * 添加分类页面
     * @param Request $request
     * @return array
     * @throws \App\Exceptions\RequestFailException
     */
    public function addCatePage(Request $request)
    {
        if($request->isMethod('get'))
            return view('NewAdmin.setting.add_cate_page');
        $postData = $request->except('_token');
        if ($request->isMethod('post')) {
            $res = $this->appLayerExec(SettingCategory::class, 'addCategory', $postData, false);
            return $res['status'] == SUCCESS_STATUS ?
                ['status' => 0, 'msg' => '添加成功'] :
                ['status' => -1, 'msg' => $res['res']['autoRes']];
        }
        return ['status' => -1, 'msg' => '请求方式错误'];
    }

    /**
     * 添加配置
     * @param Request $request
     */
    public function addSetting(Request $request, $id)
    {
        if($request->isMethod('get'))
            return view('NewAdmin.setting.add_setting_page', ['pid' => $id]);
        if ($request->isMethod('post')) {
            $postData = $request->except('_token');
            $postData['category_id'] = $id;
            $res = $this->appLayerExec(\App\Modules\Setting\Setting::class, 'addSet', $postData, false);
            return $res['status'] == SUCCESS_STATUS ?
                ['status' => 0, 'msg' => '添加成功'] :
                ['status' => -1, 'msg' => $res['res']['autoRes']];
        }
        return ['status' => -1, 'msg' => '请求方式错误'];
    }
}
